package com.idopartx.phonelightning.ui.filter

import android.content.Context
import android.text.InputFilter
import android.text.Spanned
import android.widget.Toast

class MaxTextLengthFilter(private val context: Context, private val mMaxLength: Int) :
    InputFilter {
    override fun filter(
        source: CharSequence,
        start: Int,
        end: Int,
        dest: Spanned,
        dstart: Int,
        dend: Int
    ): CharSequence? {
        val keep = mMaxLength - (dest.length - (dend - dstart))
        if (keep < end - start) {
            Toast.makeText(context, "已超过最大字符", Toast.LENGTH_SHORT).show()
        }
        return when {
            keep <= 0 -> {
                ""
            }
            keep >= end - start -> {
                null
            }
            else -> {
                source.subSequence(start, start + keep)
            }
        }
    }
}